package algorithm;

/**
 * Created by Administrator on 2017/7/5.
 * 打印double型的数据，
 * 考虑double涉及到 小数 以及 负数
 * 难以解决的是double精度的问题，可能我的方法是错误的
 */
public class PrintNum {
    //打印double型数据
    public static void printDouble(double d){
        if (d<0){
            d = -d;
            System.out.print("-");
        }
        printOut(d);
    }

    public static void printOut(double d){
        printInteger(Math.floor(d));
        if (Math.ceil(d)==d){
            return;}
        else{
           // System.out.println("差距"+(d-Math.floor(d)));

            double point = d-Math.floor(d);
            System.out.print(".");
            printPoint(point);
        }
    }

    public static void printPoint(double d){
        if (d>1||d<0) return;
        while(Math.floor(d)!=d){
            d *= 10;
//            System.out.println(d);
        }
        printInteger(d);
    }

    private static void printDigit(int n){
        if (n<10&&n>=0&&Math.ceil(n)==n)
            System.out.print(n);
        else
            return;
    }

    public static void printInteger(double d){
        if (d>=10)
            printInteger(d/10);
        printDigit((int)(d%10));

    }

    public static void main(String[] args) {
//        printDigit(2);
//        System.out.println();
//        printInteger(12324);
//        System.out.println();
//        printPoint(0.1452334);
        printOut(23.4556);
        System.out.println();
        printDouble(-123.345345346);
    }
}
